package com.rytx.system.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.rytx.common.core.domain.model.ArtUser;
import com.rytx.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rytx.system.mapper.ArtUserMapper;
import com.rytx.system.service.IArtUserService;

/**
 * 用户信息Service业务层处理
 * 
 * @author rytx
 * &#064;date  2024-04-29
 */
@Service
public class ArtUserServiceImpl implements IArtUserService
{
    @Autowired
    private ArtUserMapper artUserMapper;

    /**
     * 查询用户信息
     * 
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public ArtUser selectArtUserById(Long id)
    {
        return artUserMapper.selectArtUserById(id);
    }

    @Override
    public ArtUser selectArtUserByAliOpenid(String aliOpenid) {
        return artUserMapper.selectArtUserByAliOpenid(aliOpenid);
    }

    @Override
    public ArtUser selectArtUserByPhone(String phone) {
        return artUserMapper.selectArtUserByPhone(phone);
    }

    /**
     * 查询用户信息列表
     * 
     * @param artUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<ArtUser> selectArtUserList(ArtUser artUser)
    {
        return artUserMapper.selectArtUserList(artUser);
    }
    @Override
    public List<ArtUser> selectArtUserListShort(ArtUser artUser)
    {
        return artUserMapper.selectArtUserList(artUser);
    }

    /**
     * 新增用户信息
     * 
     * @param artUser 用户信息
     * @return 结果
     */
    @Override
    public int insertArtUser(ArtUser artUser)
    {
        artUser.setCreateTime(DateUtils.getNowDate());
        return artUserMapper.insertArtUser(artUser);
    }

    /**
     * 修改用户信息
     * 
     * @param artUser 用户信息
     * @return 结果
     */
    @Override
    public int updateArtUser(ArtUser artUser)
    {
        artUser.setUpdateTime(DateUtils.getNowDate());
        return artUserMapper.updateArtUser(artUser);
    }

    @Override
    public int updateArtUserByAliOpenid(ArtUser artUser) {
        return artUserMapper.updateArtUserByAliOpenid(artUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteArtUserByIds(Long[] ids)
    {
        return artUserMapper.deleteArtUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteArtUserById(Long id)
    {
        return artUserMapper.deleteArtUserById(id);
    }

    /**
     *
     * @return
     */
    @Override
    public Map<String, Integer> getWeekArtUserCount() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusDays(7).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        System.out.println(startDate);
        System.out.println(endDate);
        Map<String ,Integer> map = new HashMap<>();
        List<ArtUser> artUsers = artUserMapper.selectArtUserListInDate(startDate, endDate);
        for (ArtUser artUser : artUsers) {
            String k = DateUtils.parseDateToStr("yyyy-MM-dd", artUser.getCreateTime());
            Integer val = map.get(k);
            map.put(k, val == null? 0 : val + 1);
        }
        return map;
    }

    @Override
    public Map<String, Integer> getMonthArtUserCount() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusDays(30).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,Integer> map = new HashMap<>();
        List<ArtUser> artUsers = artUserMapper.selectArtUserListInDate(startDate, endDate);
        for (ArtUser artUser : artUsers) {
            String k = DateUtils.parseDateToStr("yyyy-MM-dd", artUser.getCreateTime());
            Integer val = map.get(k);
            map.put(k, val == null? 0 : val + 1);
        }
        return map;
    }

    @Override
    public Map<String, Integer> getYearArtUserCount() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDate = now.minusMonths(12).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endDate = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        Map<String ,Integer> map = new HashMap<>();
        List<ArtUser> artUsers = artUserMapper.selectArtUserListInDate(startDate, endDate);
        for (ArtUser artUser : artUsers) {
            String k = DateUtils.parseDateToStr("yyyy-MM", artUser.getCreateTime());
            Integer val = map.get(k);
            map.put(k, val == null? 0 : val + 1);
        }
        return map;
    }

    @Override
    public ArtUser selectArtUserByWxOpenid(String openid) {
        return artUserMapper.selectArtUserByWxOpenid(openid);
    }
}
